using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Diagnostics;
//
using CommandLineArgsUtilities;
//
//using SSK.SSKClient.ClientStorage;
//using SSK.SSKClient.SSKForms;
//using SSK.SSKClient.UIHelper;

namespace vXCopy
{

	public class CommandLineArguments
	{
		[CommandLineArgument(CommandLineArgumentType.DirectoryRequired, ShortName = "1", MaxType = 2)]
		public string SourcePath = null;
		[CommandLineArgument(CommandLineArgumentType.DirectoryRequired, ShortName = "2", MaxType = 2)]
		public string DestinationPath = null;

		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "T", LongName = "ThreadNumber")]
		public int ThreadNumber = 6;
		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "S", LongName = "SubFolder")]
		public bool RecurseFolder = false;

		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "F", LongName = "ForceCopy")]
		public bool ForceCopy = false;

		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "ID", LongName = "IncludeDirectories")]
		public string IncludeFolders = "*.*";
		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "IF", LongName = "IncludeFiles")]
		public string IncludeFiles = "*.*";

		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "XD", LongName = "ExcludeDirectories")]
		public string ExcludeFolders = "";
		[CommandLineArgument(CommandLineArgumentType.AtMostOnce, ShortName = "XF", LongName = "ExcludeFiles")]
		public string ExcludeFiles = "";
	}

	class Program 
	{

		internal static frmFileProcess ofrmMain = null;

		private static CommandLineArguments arguments;
		private static frmSplash oSplashForm;
		private static bool bErrorProcess = false;


		private static bool ParseArguments(string[] args)
		{
			arguments = new CommandLineArguments();
			return CommandLineArgsUtilities.Utility.ParseCommandLineArguments(args, arguments, new ErrorReporter(ReportError));
		}

		private static void ReportError(string message)
		{
			Console.WriteLine(message);
		}

		[STAThread]
		static void Main(string[] args)
		{

			try
			{
				if (args.Length != 0 && !ParseArguments(args))
				{
					DisplayUsage();
					return;
				}
				else
				{
					Debug.WriteLine("@vXCopy: Environment: " + Environment.CommandLine);
					Debug.WriteLine("@vXCopy: Args count: " + args.Length.ToString());

					ofrmMain = new frmFileProcess(args, arguments);

					oSplashForm = new frmSplash(ofrmMain);
					System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(oSplashForm.ShowForm));
					Application.DoEvents();


					//DataStore ds = new DataStore(ofrmMain);
					//ds.DataStoreFailed += new EventHandler<ProcessingFailedEvent>(DataStore_ProccessingFailed);
					////oSplashForm.StatusText.Text = "Initializing Data store...";
					//Debug.WriteLine("Initializing Data store...");
					//Application.DoEvents();

					////wait until the web service if finished processing
					//while (ds.GetProductState == ProductState.WebServiceInProgress)
					//{
					//    if (bErrorProcess)
					//        break;
					//    System.Threading.Thread.Sleep(1000);
					//}

					//if (ds.GetProductState == ProductState.NoDataStore)
					//{
					//    //notify the user that there was a problem in initializing the app, and verify that they
					//    //have internet connectivity
					//}

					//if (ds.GetProductState != ProductState.NoDataStore)
					//{
					//    //oSplashForm.StatusText.Text = "Data store initialized";
					//    Debug.WriteLine("Data store initialized");
					//    //System.Threading.Thread.Sleep(2500);
					//    //start the splash screen closing
					//    //oSplashForm.Close();
					//}
					//else
					//{
					//    //oSplashForm.StatusText.Text = "Error initializing datastore";
					//    Debug.WriteLine("Error initializing datastore");
					//    //System.Threading.Thread.Sleep(2500);
					//    //oSplashForm.Close();
					//}



					Application.EnableVisualStyles();
					Application.Run(ofrmMain);

				}

			}
			catch (Exception ex)
			{
				Debug.WriteLine("Program::Main:Error: " + ex.Message);
				MessageBox.Show(ex.Message);

			}
			finally
			{
			}

		}

		private static void DisplayUsage()
		{
			// User did not provide enough parameters. 
			// Display usage: 
			StringBuilder sb = new StringBuilder();

			Version version = Assembly.GetExecutingAssembly().GetName().Version;
			string title = String.Format("vXCopy - version {0}.{1}.{2}.{3} (beta) - Copyrights 2005 - VrDotNet - Vincent RITHNER - All Rights Reserved", version.Major, version.Minor, version.Build.ToString(), version.Revision);

			Char mychar = '-';
			sb.Append(new string(mychar, (int)(title.Length * 1.5)));
			sb.Append("\n");
			sb.Append(title);
			sb.Append("\n");
			sb.Append(new string(mychar, (int)(title.Length * 1.5)));
			sb.Append("\n\n");
			sb.Append("Copy Folder/Files Recursively in MultiTreads mode");
			sb.Append("\n\n");
			sb.Append("vxCopy FromFolder DestFolder [Thread #]");
			sb.Append("Usage: vxCopy FromFolder DestFolder /T:6 /S /F /IF *.* /ID *.* /XF *64*.* /XD AMD");
			sb.Append("\n");
			sb.Append("Thread # (Min= 1 / Max=15)-(Default= 4");
			sb.Append("\n\n");
			sb.Append((new CommandLineArgumentParser(typeof(CommandLineArguments), new ErrorReporter(ReportError))).Usage);

			MessageBox.Show(sb.ToString());
		}

		//static void DataStore_ProccessingFailed(object sender, ProcessingFailedEvent e)
		//{
		//    MessageBox.Show(e.FailureInformation.Message);
		//    //bErrorProcess = true;
		//    //oSplashForm.Close();
		//}
	
	}
}
